package com.dev.jwt.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dev.jwt.model.emum.ResponseEnum;
import com.dev.jwt.model.pojo.User;
import com.dev.jwt.utils.R;
import com.dev.jwt.utils.TokenUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *  登录控制器类
 */
@RestController
@RequestMapping("/user")
public class LoginController {
    @PostMapping("/login")
    public R<User> login(String userName, String password) {
        if (StringUtils.isNotBlank(userName) && StringUtils.isNotBlank(password)) {
            if ("张三".equals(userName) && "123456".equals(password)) {
                User user = new User();
                JSONObject json = JSONUtil.createObj()
                        .put("name", "zhangsan");
                String token = TokenUtil.createToken(json);
                user.setToken(token);
                return R.ok(user);
            }
        }
        return R.error(ResponseEnum.USERNAME_PASSWORD_ERROR);
    }
}
